<template>
    <div class="card">
        <div class="overview-container">
            <div class="cardTitle">
                <div>
                    <span class="title">今日数据</span>
                    <span class="time">2021.45.63</span>
                </div>

                <RouterLink to="/order">
                    <span class="goto">详细数据</span>
                </RouterLink>
            </div>
            <div class="overview-container">
                <ul>
                    <li>
                        <div><span class="title">营业额</span>
                            <div class="buttom">￥{{ businessData.turnover }}</div>
                        </div>
                    </li>
                    <li>
                        <div><span class="title">有效订单</span>
                            <div class="buttom"> {{ businessData.validOrderCount }}</div>
                        </div>
                    </li>
                    <li>
                        <div><span class="title">订单完成率</span>
                            <div class="buttom">{{ businessData.orderCompletionRate * 100 }}%</div>
                        </div>
                    </li>
                    <li>
                        <div><span class="title">平均客单价</span>
                            <div class="buttom">￥{{ businessData.unitPrice }}</div>
                        </div>
                    </li>
                    <li>
                        <div><span class="title">新增用户</span>
                            <div class="buttom">{{ businessData.newUsers }}</div>
                        </div>
                    </li>

                </ul>
            </div>
        </div>
    </div>

    <div class="card">
        <div class="cardTitle">
            <div>
                <span class="title">订单管理</span>
                <span class="time">2024.01.00</span>
            </div>

            <RouterLink to="/order">
                <span class="goto">订单明细</span>
            </RouterLink>

        </div>

        <div class="overview-container">
            <ul>
                <li>
                    <span class="title">待接单</span>
                    <span class="num right danger">{{ overviewOrdersData.waitingOrders }}</span>
                </li>
                <li>
                    <span class="title">待派送</span>
                    <span class="num right danger">{{ overviewOrdersData.deliveredOrders }}</span>
                </li>
                <li>
                    <span class="title">已完成</span>
                    <span class="num right ">{{ overviewOrdersData.completedOrders }}</span>
                </li>
                <li>
                    <span class="title">已取消</span>
                    <span class="num right ">{{ overviewOrdersData.cancelledOrders }}</span>
                </li>
                <li>
                    <span class="title">全部订单</span>
                    <span class="num right ">{{ overviewOrdersData.allOrders }}</span>
                </li>
            </ul>
        </div>

    </div>
    <div class="card-container">
        <div class="card">
            <div class="cardTitle">
                <div>
                    <span class="title">菜品总览</span>
                </div>

                <RouterLink to="/dish">
                    <span class="goto">菜品管理</span>
                </RouterLink>

            </div>
            <div class="overview-container">
                <ul>
                    <li><span><svg-icon style="cursor: pointer;" name="yes" width="25px" height="25px"
                                color="black"></svg-icon>已起售</span><span class="num">{{ overviewSetmealsData.sold
                            }}</span></li>
                    <li><span><svg-icon style="cursor: pointer;" name="no" width="25px" height="25px"
                                color="black"></svg-icon>已停售</span><span class="num">{{
                                overviewSetmealsData.discontinued }}</span></li>
                    <li class="add">
                        <svg-icon style="cursor: pointer;" name="plus" width="25px" height="25px"
                            color="black"></svg-icon>
                        新增菜品
                    </li>
                </ul>
            </div>
        </div>
        <div class="card">
            <div class="cardTitle">
                <div>
                    <span class="title">套餐总览</span>
                </div>

                <RouterLink to="/order">
                    <span class="goto">套餐管理</span>
                </RouterLink>

            </div>
            <div class="overview-container">
                <ul>
                    <li>

                        <span> <svg-icon style="cursor: pointer;" name="yes" width="25px" height="25px"
                                color="black"></svg-icon>已起售</span><span class="num">{{ overviewDishesData.sold
                            }}</span>
                    </li>
                    <li>

                        <span>
                            <svg-icon style="cursor: pointer;" name="no" width="25px" height="25px"
                                color="black"></svg-icon>已停售</span><span class="num">{{ overviewDishesData.discontinued
                            }}</span>
                    </li>
                    <li class="add">
                        <svg-icon style="cursor: pointer;" name="plus" width="25px" height="25px"
                            color="black"></svg-icon>
                        新增套餐
                    </li>
                </ul>
            </div>
        </div>
    </div>
    <div class="card">
        <div class="cardTitle">
            <span class="title">订单信息</span>
            <FilterList :filterData="filterData" @filterClick="filterClickHandle"></FilterList>
        </div>
        <el-table class="home-table" :data="tableData" style="width: 100%" fit>
            <el-table-column prop="number" label="订单号" width="180" align="center"> </el-table-column>
            <el-table-column prop="orderDishes" label="订单菜品" width="180" align="center"> </el-table-column>
            <el-table-column prop="address" label="地址" align="center"> </el-table-column>
            <el-table-column prop="estimatedDeliveryTime" label="预计送达时间" align="center"> </el-table-column>
            <el-table-column prop="amount" label="实收金额" align="center" min-width="50"> </el-table-column>
            <el-table-column prop="remark" label="备注" align="center"> </el-table-column>
            <el-table-column label="操作" align="center">
                <template #default="scope">
                    <el-link v-if="scope.row.status == 3" type="primary"
                        @click="deliveryHandel(scope.$index, scope.row)">派送</el-link>
                    <el-link v-if="scope.row.status == 2" type="primary"
                        @click="acceptHandel(scope.$index, scope.row)">接单</el-link>
                    <el-link v-if="scope.row.status == 2" type="danger"
                        @click="refuseHandel(scope.$index, scope.row)">拒单</el-link>
                    <el-link type="primary" @click="detailHandel(scope.$index, scope.row)">查看</el-link>
                </template>
            </el-table-column>
        </el-table>
    </div>

    <el-dialog v-model="dialogFormVisible" title="拒绝原因" width="42%">
        <el-form :model="rejectForm" label-width="90px" :label-position="labelPosition">
            <el-form-item label="拒绝原因" label-width="90px">
                <el-select v-model="rejectForm.rejectionReason" placeholder="选择拒绝原因">
                    <el-option label="订单较多，暂无法接单" value="订单较多，暂无法接单" />
                    <el-option label="菜品已售完" value="菜品已售完" />
                    <el-option label="已打烊" value="已打烊" />
                    <el-option label="自定义原因" value=" " />
                </el-select>
            </el-form-item>
            <el-form-item v-if="!['订单较多，暂无法接单', '菜品已售完', '已打烊', ''].includes(rejectForm.rejectionReason)" label="原因：">
                <el-input v-model="rejectForm.rejectionReason" type="textarea" :placeholder="'请填写您的原因（限20字内）'"
                    maxlength="20" />
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取消</el-button>
                <el-button type="primary" @click="confirmRefuseHandel">确认</el-button>
            </span>
        </template>
    </el-dialog>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import {
    conditionSearch,
    getBusinessData,
    getOverviewSetmeals,
    getOverviewDishes,
    getOverviewOrders,
    confirm,
    rejection,
    delivery
} from '@/api/admin/order/index'
import {
    type conditionQuery,
    type businessDataRes,
    type overviewOrdersRes,
    type overviewSetmealsDataRes,
    type DishOverViewVO,
    type OrdersRejectionDTO
} from '@/api/admin/order/type'

const businessData = ref<businessDataRes>({
    turnover: 0,
    validOrderCount: 0,
    orderCompletionRate: 0,
    unitPrice: 0,
    newUsers: 0,
})
const overviewOrdersData = ref<overviewOrdersRes>({
    allOrders: 0,
    cancelledOrders: 0,
    completedOrders: 0,
    deliveredOrders: 0,
    waitingOrders: 0
})
const overviewSetmealsData = ref<overviewSetmealsDataRes>({
    discontinued: 0,
    sold: 0
})
const overviewDishesData = ref<DishOverViewVO>({
    discontinued: 0,
    sold: 0
})
const dialogFormVisible = ref(false)
const rejectForm = ref<OrdersRejectionDTO>({
    id: '',
    rejectionReason: ''
})
const tableTotal = ref(0)
const tableData = ref([])
const labelPosition = ref('right')
const condition = ref<conditionQuery>({
    beginTime: '',
    endTime: '',
    number: '',
    page: 1,
    pageSize: 10,
    phone: '',
    status: '2'
})
onMounted(() => {
    handleSearch()
    handlegetBusinessData()
    handlegetOverviewSetmeals()
    handlegetOverviewDishes()
    handlegetOverviewOrders()
})
const handleSearch = async () => {
    const res = await conditionSearch(condition.value)
    tableTotal.value = res.data.total;
    tableData.value = res.data.records;

}
const filterData = [
    {
        index: 1,
        label: '待接单'
    },
    {
        index: 2,
        label: '待派送'
    }
]
const deliveryHandel = async (index: number, row: any) => {
    const res = await delivery(row.id)
    if (res.code == 1) {
        //重写获取订单列表
        handleSearch()
        ElMessage.success('操作成功')
    }
}
const acceptHandel = async (index: number, row: any) => {
    const res = await confirm(row.id)
    if (res.code == 1) {
        //重写获取订单列表
        handleSearch()
        ElMessage.success('操作成功')
    }
}
const refuseHandel = (index: number, row: any) => {
    dialogFormVisible.value = true
    rejectForm.value.id = row.id
    rejectForm.value.rejectionReason = ''
}
const confirmRefuseHandel = async () => {
    if (['', ' '].includes(rejectForm.value.rejectionReason)) {
        ElMessage.warning('请选择拒绝原因')
        return
    }
    const res = await rejection(rejectForm.value)
    if (res.code == 1) {
        ElMessage.success('操作成功')
        dialogFormVisible.value = false
        handleSearch()
    }
}
const detailHandel = (index: number, row: any) => {
    console.log(index, row);
}
const filterClickHandle = (text: string) => {
    if (text === '待接单') condition.value.status = '2'
    if (text === '待派送') condition.value.status = '3'
    handleSearch()
}
const handlegetBusinessData = async () => {
    const res = await getBusinessData()
    businessData.value = res.data
}
const handlegetOverviewOrders = async () => {
    const res = await getOverviewOrders()
    overviewOrdersData.value = res.data
}
const handlegetOverviewSetmeals = async () => {
    const res = await getOverviewSetmeals()
    overviewSetmealsData.value = res.data
}
const handlegetOverviewDishes = async () => {
    const res = await getOverviewDishes()
    overviewDishesData.value = res.data
}

</script>
<style lang="scss">
.home-table.el-table td.el-table__cell div {
    font-size: 12px;

    .el-link__inner {
        font-size: 12px;
        margin-left: 10px
    }
}
</style>